﻿using AjaxControlToolkit;
using SGA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SGA.Supporters
{
    public partial class BrowseSupporter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            //3000ms = 3s
            System.Threading.Thread.Sleep(3000);
            BindData();            
        }

        private void BindData()
        {
            LVSupporters.DataSourceID = "EDSSupporter";
            LVSupporters.DataBind();
        }

        private void UnbindData()
        {
            LVSupporters.DataSourceID = null;
            LVSupporters.DataBind();
        }

        protected void ClearButton_Click(object sender, EventArgs e)
        {
            UnbindData();
            IdSupportersTextBox.Text = string.Empty;
            NameTextBox.Text = string.Empty;
            AddressTextBox.Text = string.Empty;
            PostalCodeLocalTextBox.Text = string.Empty;
            PostalCodeStreetTextBox.Text = string.Empty;
            ContactTypesDropDownList.SelectedValue = string.Empty;
            ContactTextBox.Text = string.Empty;
            IdDonationSituationsDropDownList.SelectedValue = string.Empty;
        }

        protected void ContactTypesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((DropDownList)sender).SelectedValue == string.Empty)
            {
                ContactTextBox.Enabled = false;
            }
            else
            {
                ContactTextBox.Enabled = true;
            }
        }

        protected void FilterByContact_Querying(object sender, System.Web.UI.WebControls.Expressions.CustomExpressionEventArgs e)
        {
            if (ContactTextBox.Text != string.Empty)
            {
                using (var bd = new MailingEntities())
                {
                    SGA.Model.SupporterContacts queryContact = (from contact in bd.SupporterContacts where contact.Designation == ContactTextBox.Text select contact).SingleOrDefault();

                    if (queryContact != null)
                    {
                        e.Query = from supporter in e.Query.Cast<SGA.Model.Supporters>()
                                  where supporter.Id == queryContact.IdSupporters
                                  select supporter;
                    }
                    else
                    {
                        
                        e.Query = from supporter in e.Query.Cast<SGA.Model.Supporters>()
                                  where supporter.Id == 0
                                  select supporter;
                    }

                }
            }
        }


        protected void LVSupporters_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                Response.Redirect("EditSupporter.aspx?Id=" + id);
            }
        }

    }
}